/**
 * 
 */
package teehee.DB;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;

import android.content.Context;

/**
 * @author Shubham
 *
 */
public class DBHealth {

	private String date;
	private String weight;
	private static ArrayList<DBHealth> db_health = new ArrayList<DBHealth>();
	
	private DBHealth(String date, String weight)
	{
		this.date = date;
		this.weight = weight;
	}

	/**
	 * @return the date
	 */
	public String getDate() {
		return date;
	}

	/**
	 * @return the weight
	 */
	public String getWeight() {
		return weight;
	}
	
	
	public static String getHeight(Context ctx)
	{
		String height = "0";
		try{
		FileInputStream fis = ctx.openFileInput("height");
	    height = readFileByte(fis);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return height;
	}
	
	public static void updateHeight(String height, Context ctx)
	{
		try{
		FileOutputStream fos = ctx.openFileOutput("height", Context.MODE_PRIVATE);
		fos.write(height.getBytes());
		fos.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	private static void parseHistory(String db, String date)
	{
		DBHealth entry = new DBHealth(date, db);
		db_health.add(entry);
	}
	
	
	private static void writeToHistoryFile(Context ctx, String file, DBHealth entry)
	{
		try{
		FileOutputStream fos = ctx.openFileOutput(file, Context.MODE_PRIVATE);
		fos.write(entry.getWeight().getBytes());
		fos.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private static String readFileByte(FileInputStream fis) throws FileNotFoundException
	{
		String db = "";
		try {
			Scanner s = new java.util.Scanner(fis);
	        db = s.useDelimiter("\\A").next();
	    } catch (java.util.NoSuchElementException e) {
	         e.printStackTrace();
	    }

		return db;
	}
	
	
	
	public static ArrayList<DBHealth> getHealthHistory()
	{
		return new ArrayList<DBHealth>(db_health);
	}
	
	public static void createDB(Context ctx)
	{
		try{
		FileOutputStream fos = ctx.openFileOutput("06012012health", Context.MODE_PRIVATE);
		String wt = "157";
		fos.write(wt.getBytes());
		fos.close();
		
		fos = ctx.openFileOutput("06022012health", Context.MODE_PRIVATE);
		wt = "158";
		fos.write(wt.getBytes());
		fos.close();
		
		fos = ctx.openFileOutput("06032012health", Context.MODE_PRIVATE);
		wt = "159";
		fos.write(wt.getBytes());
		fos.close();
		
		fos = ctx.openFileOutput("06042012health", Context.MODE_PRIVATE);
		wt = "160";
		fos.write(wt.getBytes());
		fos.close();
		
		fos = ctx.openFileOutput("height", Context.MODE_PRIVATE);
		wt = "75";
		fos.write(wt.getBytes());
		fos.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	@SuppressWarnings("static-access")
	public static void instantiateHealthHistoryDB(Context ctx)
	{
		try
		{
			db_health = new ArrayList<DBHealth>();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("MMddyyyy");
		String date = format.format(c.getTime());
		String fileName = date+"health";
		File f = ctx.getFileStreamPath(fileName);
		if(!f.exists())
		{
			
			FileOutputStream fos = ctx.openFileOutput(fileName, Context.MODE_PRIVATE);
			String wt = "0";
			fos.write(wt.getBytes());
			fos.close();
		}
		
		FileInputStream fs = ctx.openFileInput("date");
		String firstDate = readFileByte(fs);
		fs.close();
		createDB(ctx);
		while(!date.equals(firstDate))
		{
			try{
			FileInputStream fis = ctx.openFileInput(fileName);
			String db = readFileByte(fis);
			parseHistory(db, date);
			fis.close();
			c.add(c.DATE, -1);
			date = format.format(c.getTime());
			fileName = date+"health";
		}
		catch(Exception e)
		{
			e.printStackTrace();
			c.add(c.DATE, -1);
			date = format.format(c.getTime());
			fileName = date+"health";
		}
		}
		}
		
		
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	
	
	public static boolean createNewEntry(Context ctx, String weight)
	{
		boolean success = true;
		Calendar c = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("MMddyyyy");
		String date = format.format(c.getTime());
		DBHealth newEntry = new DBHealth(date, weight);
		
			if(!weight.equals("0") && weight.length()>=2)
			{
				db_health.remove(0);
				db_health.add(0,newEntry);
				String fileName = date +"health";
				writeToHistoryFile(ctx, fileName, newEntry);
				return success;
			}
			else
			{
				return !success;
			}
		
	}
	
	
	public static int getSize()
	{
		return db_health.size();
	}
	
	
}
